﻿using Autofac;
using log4net;
using Module = Autofac.Module;

namespace ToolMonitoring.WebApi.Dependency
{
	public class AutofacModuleRegister:Module
	{
		protected override void Load(ContainerBuilder builder) 
		{
			var assembly = Assembly.GetEntryAssembly();
			builder.RegisterAssemblyTypes(assembly)
					.Where(t=> !t.IsAbstract)
					.Where(t=> t.GetInterfaces().Contains(typeof(ISingletonDependency)))
					.AsImplementedInterfaces()
					.AsSelf()
					.SingleInstance()
					.PropertiesAutowired();

			builder.RegisterAssemblyTypes(assembly)
					.Where(t => !t.IsAbstract)
					.Where(t => t.GetInterfaces().Contains(typeof(ITransientDependency)))
					.AsImplementedInterfaces()
					.AsSelf()
					.InstancePerDependency()
					.PropertiesAutowired();

			// builder.RegisterCallback(PropertyInjector.)


			builder.Register<ILogger>(c => {
				
				var lf = c.Resolve<ILoggerFactory>();
				// ToolMonitoring.WebApi.Services.AuditLogService - Find
				return lf.CreateLogger("APP_LOG");
			}).SingleInstance();

		}
	}
}
